package com.hy.convenient.api.controller.down;

import com.hy.convenient.api.controller.conInterface.DownControllerInterface;
import com.hy.convenient.api.pojo.VipDatePojo;
import com.hy.convenient.api.pojo.VipLevel;
import com.hy.convenient.api.pojo.check.CheckUpdateCheckPojo;
import com.hy.convenient.api.pojo.check.VipDateCheckPojo;
import com.hy.convenient.api.server.VipDownServer;
import com.hy.convenient.api.server.VipLevelServer;
import com.hy.convenient.common.LayuiToTable;
import com.hy.convenient.common.VipEnum;
import com.hy.convenient.tool.MD5;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 会员卡接口查询
 * @className VipController
 * @Author hdy
 * @Date 2020-12-25 15:38
 * @Version 1.0
 */
@RestController
public class VipController implements DownControllerInterface<VipDateCheckPojo> {
    @Autowired
    private VipDownServer vip;
    @Autowired
    private VipLevelServer vipLevelServer;

    @Override
    @PostMapping("/api/vipQuery")
    public Map<String, Object> downData(@RequestBody VipDateCheckPojo t) {
//        if (StringUtils.isEmpty(t.getVipNo())) {
//            return LayuiToTable.business_fail("会员查询标识不能为空");
//        }
        VipEnum viptype = VipEnum.valueOf(t.getVipType());
        if (viptype == null) {
            return LayuiToTable.business_fail("传入的会员卡类型");
        }
//        String str = checkUpdate(t);
//        if (!StringUtils.isEmpty(str)) {
//            return LayuiToTable.business_fail(str);
//        }
//        if (!MD5.check(t.getMerId() + t.getCwbm() + t.getDateTime(), t.getSign())) {
//            return LayuiToTable.business_fail("签名失败");
//        }
        Map<String, Object> map = new HashMap<>();
        map.put("merId", t.getMerId());
        map.put("vipNo", t.getVipNo());
//        查会员卡信息
        VipDatePojo vipDatePojo = vip.queryVip(map, viptype);
        map.put("HYKH", vipDatePojo.getHykh());
//        更具会员卡卡号查询 积分金额等信息
        VipDatePojo vipDatePojo01 = vip.queryVipData(map, viptype);
        vipDatePojo.setDqje(vipDatePojo01.getDqje());
        vipDatePojo.setDqjf(vipDatePojo01.getDqjf());
        map.clear();
        if (StringUtils.isNotEmpty(vipDatePojo.getKdj())) {
            map.put("merId", t.getMerId());
            map.put("djbm", vipDatePojo.getKdj());
            VipLevel vipLevel = vipLevelServer.queryPojo(map);
//            老系统和没有该值的时候，默认值是0
            vipDatePojo.setBirthdayBs(vipLevel ==null?0:vipLevel.getBirthdayBs());
        }

        List<VipDatePojo> lists = new ArrayList<>();
        lists.add(vipDatePojo);
        return LayuiToTable.layuiToTable(lists, lists.size());
    }

    @Override
    public Map<String, Object> checkUpdateDateDown(@RequestBody CheckUpdateCheckPojo t) {
        return null;
    }
}
